// cd /projects/hsieh_project/proj_201809/code_2_202011/
// qstata city_mgrow_sales.do &

set linesize 255
capture log close
log using /projects/hsieh_project/proj_201809/code_2_202011/city_mgrow_sales_log, replace text

clear all
cd /projects/

di "Started at $S_TIME $S_DATE"

global rev_date: display %tdYYNNDD date("$S_DATE", "DMY")
di "${rev_date}"

global dir_proj "/projects/hsieh_project/proj_201809/"

global dir_do "${dir_proj}/code_2_202011/"
global dir_data "${dir_proj}/data/"
global dir_out "${dir_proj}/output/202011_main/"
noi capture mkdir ${dir_out}
global dir_outf "${dir_out}/city_mgrow_sales/"
noi capture mkdir ${dir_outf}
noi capture mkdir "${dir_outf}/data/"

global year1 = 1977
global year2 = 2013

global gl_perc = "1 10"
local l_perc = "1 10"

do "/projects/hsieh_project/code_0_general/f_rounding.do"
do "/projects/hsieh_project/code_0_general/f_reg.do"

global ds_ind "${dir_data}/ind_sum_all"

//==============================================================================
/*
Author: Adarsh Kumar
Objective: Missing growth using Sales.

1. Version 1: all available ch_inds from 1977-2012
2. Version 2: all available ch_inds from 1992-2012

*/

use "${dir_data}/cityind_sum_msa1983_all", clear
collapse (sum) emp=emp_cind, by (msa1983 year)
bys year: egen total_emp = total(emp)
gen emp_sh = emp / total_emp 

preserve
replace year = year - 5
rename emp_sh emp_sh_future
rename emp emp_future
rename total_emp total_emp_future
save "${dir_outf}/data/emp_msa1983.dta", replace
restore
merge 1:1 year msa1983 using "${dir_outf}/data/emp_msa1983.dta"

keep if _merge == 3
drop _merge 
gen emp_sh_final = (emp_sh_future + emp_sh) / 2

save "${dir_outf}/data/weights_emp_msa1983.dta", replace

program mgrow
	args drop_y1 sigma ver
	local sigma_2 = 1 / (`sigma' - 1)
	di `sigma_2'
	local y1 = `drop_y1' + 5
	
	drop if year == `drop_y1' | year == 2012
	*Dropping msas with small response rates in EC */
	rename msa msa1983
	merge m:1 msa1983 using "${dir_out}/city_sales_top_mkt_firms/dropped_msa1983_sales", keepusing(tag*) nogen
	tab msa1983 if tag_small_emp_w == 1
	drop if tag_small_emp_w == 1
	drop tag*
	
	/* Missing = 0 */
	replace inc_missing = 0 if inc_missing == .
	replace newshare = 0 if newshare == .
	gen inc_missing_2 = inc_missing * `sigma_2'
	sum inc_missing*
	gen total_mi_growth = inc_missing_2 + newshare 
	
	preserve
	collapse (mean) inc_missing_2 newshare total_mi_growth emp_msa83_1977, by(msa1983)
	gen ln_emp_msa1983_1977 = ln(emp_msa83_1977)
	
	*Rounding raw values to 4sf for plot
	gen ln_emp_msa1983_1977_4sf = ln_emp_msa1983_1977
	gen inc_missing_2_4sf = inc_missing_2
	gen total_mi_growth_4sf = total_mi_growth
	
	rounding_4dig "ln_emp_msa1983_1977_4sf inc_missing_2_4sf total_mi_growth_4sf"
	sum ln* inc* total*
	
	twoway (lpolyci inc_missing_2_4sf ln_emp_msa1983_1977_4sf, level(99) clc(navy) clw(medthick) fc(none) alcolor(gs12)), ///
	ytitle("Missing Growth from Sales") xtitle("ln(City Employment in ${year1})") ///
	title("Incumbent Industries (`y1' - 2012)") ///
	legend(off) ///
	note("99% CI. Subsetted to industries with Establishment and Sales data from `y1' to 2012.")
graph export "${dir_outf}/lpoly_inc_miss_X_ln_c_emp_`ver'_msa1983.png", replace width(3000) height(2000)

	twoway (lpolyci total_mi_growth_4sf ln_emp_msa1983_1977_4sf, level(99) clc(navy) clw(medthick) fc(none) alcolor(gs12)), ///
	ytitle("Missing Growth from Sales") xtitle("ln(City Employment in ${year1})") ///
	title("Incumbent + New Industries (`y1' - 2012)") ///
	legend(off) ///
	note("99% CI. Subsetted to industries with Establishment and Sales data from `y1' to 2012.")
graph export "${dir_outf}/lpoly_total_miss_X_ln_c_emp_`ver'_msa1983.png", replace width(3000) height(2000)
	keep *4sf
	save "${dir_outf}/data/mgrow_plot_data_`ver'", replace
	
	*Calculate Mean Missing Growth Across MSAs 
	restore
	preserve
	
	collapse (mean) inc_missing_2 newshare total_mi_growth [aw = sales_sh_final], by (year)
	
	collapse (mean) inc_missing_2 newshare total_mi_growth
	
	outsheet using "${dir_outf}/mean_mi_growth_`ver'_sales_weight.csv", comma replace
	
	restore 
	
	merge 1:1 year msa1983 using "${dir_outf}/data/weights_emp_msa1983.dta", keepusing(emp_sh_final)
	keep if _merge == 3
	
	collapse (mean) inc_missing_2 newshare total_mi_growth [aw = emp_sh_final], by (year)
	
	collapse (mean) inc_missing_2 newshare total_mi_growth
	
	rounding_4dig "inc_missing_2 newshare total_mi_growth"
	
	outsheet using "${dir_outf}/mean_mi_growth_`ver'_emp_weight.csv", comma replace

end

use "${dir_data}/sales_mgrow_msa_v1_1977.dta", clear
mgrow 1972 3 "v1_1977" 

use "${dir_data}/sales_mgrow_msa_v2_1992.dta", clear
mgrow 1987 3 "v1_1992" 
